/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.common;

import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sss.mapreduce.Mapper;
import org.sss.mapreduce.Partitioner;
import org.sss.mapreduce.Reducer;
import org.sss.mapreduce.SssException;
import org.sss.mapreduce.datatype.Packable;

public class Utils {
  private static Logger logger = LoggerFactory.getLogger(Utils.class);

  public static Class<? extends Mapper> loadMapperClass(ClassLoader cl, String name) throws SssException {
    return loadClass(cl, name, Mapper.class);
  }

  public static Class<? extends Reducer> loadReducerClass(ClassLoader cl, String name) throws SssException {
    return loadClass(cl, name, Reducer.class);
  }

  public static Class<? extends Packable> loadPackableClass(ClassLoader cl, String name) throws SssException {
    return loadClass(cl, name, Packable.class);
  }

  public static Class<? extends Partitioner> loadPartitionerClass(ClassLoader cl, String name) throws SssException {
    return loadClass(cl, name, Partitioner.class);
  }

  private static <T> Class<? extends T> loadClass(ClassLoader cl, String name, Class<T> baseKlass) throws SssException {
    if (name == null) {
      return null;
    }
    try {
      return cl.loadClass(name).asSubclass(baseKlass);
    } catch (ClassNotFoundException e) {
      throw new SssException(e);
    }
  }

  public static <T> T newInstance(Class<? extends T> klass) throws SssException {
    try {
      return klass.newInstance();
    } catch (InstantiationException e) {
      throw new SssException(e);
    } catch (IllegalAccessException e) {
      throw new SssException(e);
    }
  }

  public static <T> void closeDataReceivers(List<? extends DataReceiver<T>> recvs) throws SssException {
    SssException exp = null;
    for (DataReceiver<T> q: recvs) {
      try {
        q.close();
      } catch (SssException e) {
        if (exp == null) {
          exp = e;
        } else if (exp.getCause() != e.getCause()) {
          logger.error("Ignore error", e);
        }
      }
    }
    if (exp != null) {
      throw exp;
    }
  }

  public static <U> CloseableIterator<U> wrap(final Iterator<U> it) {
    return new CloseableIterator<U>() {
      @Override
      public boolean hasNext() {
        return it.hasNext();
      }
      @Override
      public U next() {
        return it.next();
      }
      @Override
      public void remove() {
        it.remove();
      }
      @Override
      public void close() {
        // Do nothing
      }
    };
  }
  
}
